﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AsteroidWar
{
    public class Sprite
    {
        public Texture2D Texture;

        protected List<Rectangle> frames = new List<Rectangle>();
        private int frameWidth;
        private int frameHeight;
        private int currentFrame;
        private float frameTime = 0.1f;
        private float timeForCurrentFrame = 0.0f;

        private Color tintColor = Color.White;
        private float rotation = 0.0f;

        public int CollisionRadius = 0;
        public int BoundingXPadding = 0;
        public int BoundingYPadding = 0;

        protected Vector2 location = Vector2.Zero;
        protected Vector2 velocity = Vector2.Zero;

        
        public Sprite(Vector2 location, Texture2D Texture, Rectangle initialFrame, Vector2 velocity)
        {
            this.location = location;
            this.Texture = Texture;
            this.velocity = velocity;

            frames.Add(initialFrame);
            this.frameWidth = initialFrame.Width;
            this.frameHeight = initialFrame.Height;
        }

        public void ClearFrame()
        {
            frames.Clear();
        }

        public Vector2 Location
        {
            get { return location; }
            set { location = value; }
        }

        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        public float Rotation
        {
            get { return rotation; }
            set { rotation = value % MathHelper.TwoPi; }
        }

        public Color TintColor
        {
            get { return tintColor; }
            set { tintColor = value; }
        }

        public int Frame
        {
            get { return currentFrame; }
            set { currentFrame = (int)MathHelper.Clamp(value, 0, frames.Count - 1); }
        }

        public float FrameTime
        {
            get { return frameTime; }
            set { frameTime = MathHelper.Max(0, value); }
        }

        public Rectangle Source
        {
            get { return frames[currentFrame]; }
        }

        public Rectangle Destination
        {
            get { return new Rectangle((int)location.X, (int)location.Y, frameWidth, frameHeight); }
        }

        public Vector2 Center
        {
            get { return location + new Vector2(frameWidth / 2, frameWidth / 2); }
        }

        //The box surround sprite for calculating collisions
        public Rectangle BoundingBoxRect
        {
            get { return new Rectangle(
                (int)location.X + BoundingXPadding, 
                (int)location.Y + BoundingYPadding, 
                frameWidth - (BoundingXPadding * 2), 
                frameHeight - (BoundingYPadding * 2)); }
        }

        public bool IsBoxColliding(Rectangle OtherBox)
        {
            return BoundingBoxRect.Intersects(OtherBox);
        }

        public bool IsCircleColliding(Vector2 otherCenter, float otherRadius)
        {
            return Vector2.Distance(Center, otherCenter) < (CollisionRadius + otherRadius);
        }

        public void AddFrame(Rectangle frameRectangle)
        {
            frames.Add(frameRectangle);
        }

        public virtual void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            timeForCurrentFrame += elapsed;
            if (timeForCurrentFrame >= FrameTime)
            {
                timeForCurrentFrame = 0.0f;
                currentFrame = (currentFrame + 1) % (frames.Count);
            }
            location += (velocity * elapsed);
        }

        public virtual void Draw(SpriteBatch spriteBatch, float scale)
        {
            spriteBatch.Draw(
                Texture,
                Center,
                Source,
                tintColor,
                rotation,
                new Vector2(frameWidth/2,frameHeight/2),
                scale,
                SpriteEffects.None,
                0.0f);
        }
    }
}
